<?php
/**
 * class RestService
 *
 * @author: hqlam
*/
class RestService {
	
	private $_functions = array();
	
	public function addFunction($function) {
		array_push($this->_functions, $function);
	}

	public function handle() {
		if ($_SERVER['REQUEST_METHOD'] != 'POST') return;
		$data = json_decode(file_get_contents('php://input'), true);
		$method = $data['method'];
		$param = $data['arg'];
		
		$result = null;
		foreach ($this->_functions as $func) {
			if ($method != $func) continue;
			$result = call_user_func($func, $param);
			break;
		}

		header('Content-type: application/json');
		echo json_encode($result);	// output result
	}
}
?>